/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_Studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "PBD物理体.h"




_S_PBF流体::_S_PBF流体(S_物理材质& 材质, const S_Tranform& t, uint32 数量, uint32 key) {
	m_Count = 数量;
	m_Type = E_物理体类型::E_物理体_流体;

}

_S_PBF流体::~_S_PBF流体() {
	
}

void _S_PBF流体::f_构建粒子体素() {
}

void _S_PBF流体::f_构建粒子体素(vec3* 外部指针, float32* mess) {
	for (uint32 i = 0; i < m_Count; ++i) {
		外部指针[i] = {};
		mess[i] = 1;
	}
}

void _S_PBF流体::f_add粒子到缓存(S_PDB数据* core) {
	
}

void _S_PBF流体::f_setLocRot(const vec3& loc, const vec3& r) {
	
}

void _S_PBF流体::f_setPos(const vec3& l) {
}

void _S_PBF流体::f_setRot(const vec3& r) {
}

void _S_PBF流体::f_setVel(const vec3& l) {
}






void S_PBF物理体::f_构建粒子体素() {
}







S_PBF边界::S_PBF边界(vec3 大小, float32 间隔, const S_Tranform& t, uint32 key) {
	m_Type = E_物理体类型::E_物理体_静态;

	m_边界大小 = 大小;
	m_单元大小 = 间隔;
	f_构建粒子体素();
}

S_PBF边界::~S_PBF边界() {

}

void S_PBF边界::f_构建粒子体素() {
	auto size = m_边界大小 / m_单元大小;
	uvec3 dim = { uint32(size.x), uint32(size.y), uint32(size.z) };

	m_Points.resize(dim.x * dim.y * 2 + dim.x * dim.z * 2 + dim.y * dim.z * 2);
	auto* p = m_Points.data();

	m_Count = uint32(m_Points.size());


	vec3	中心偏移 = m_边界大小 / 2;
	中心偏移.x = -中心偏移.x;
	中心偏移.y = -中心偏移.y;
	中心偏移.z = -中心偏移.z;

	uint64 offset = 0;
	uint64 面点数量 = dim.x * dim.y;

	//============ XY =================
	for (uint32 y = 0; y < dim.y; ++y) {
		for (uint32 x = 0; x < dim.x; ++x) {
			uint64 id = y*dim.y + x;

			p[id] = { 中心偏移.x + m_单元大小 * x , 中心偏移.y + m_单元大小 * y, 中心偏移.z };
			p[id + 面点数量] = p[id];
			p[id + 面点数量].z = -中心偏移.z;
		}
	}


	//============ XZ =================
	面点数量 = dim.x * dim.z;
	offset += 面点数量 * 2;
	for (uint32 z = 0; z < dim.z; ++z) {
		for (uint32 x = 0; x < dim.x; ++x) {
			uint64 id = z * dim.z + x + offset;

			p[id] = { 中心偏移.x + m_单元大小 * x , 中心偏移.y, 中心偏移.z + m_单元大小 * z };
			p[id + 面点数量] = p[id];
			p[id + 面点数量].y = -中心偏移.y;
		}
	}


	//============ YZ =================
	面点数量 = dim.y * dim.z;
	offset += 面点数量 * 2;
	for (uint32 z = 0; z < dim.z; ++z) {
		for (uint32 y = 0; y < dim.y; ++y) {
			uint64 id = z * dim.z + y + offset;

			p[id] = { 中心偏移.x, 中心偏移.y + m_单元大小 * y, 中心偏移.z + m_单元大小 * z };
			p[id + 面点数量] = p[id];
			p[id + 面点数量].x = -中心偏移.x;
		}
	}

	
	/*for (uint32 z = 0; z < dim.z; ++z) {
		for (uint32 y = 0; y < dim.y; ++y) {
			for (uint32 x = 0; x < dim.x; ++x) {
				uint64 id = 面点数量 * z + x;

			}
		}
	}*/
}

void S_PBF边界::f_setLocRot(const vec3& loc, const vec3& r) {
}

void S_PBF边界::f_setPos(const vec3& l) {
}

void S_PBF边界::f_setRot(const vec3& r) {
}

void S_PBF边界::f_setVel(const vec3& l) {
}

S_PBF流体* f_PBF创建流体body() {
	S_PBF流体* body = (S_PBF流体*)malloc(sizeof(S_PBF流体));
	body->m_体素 = new S_体素();

	body->m_Offset = 0;
	body->m_Count = 0;
	body->m_发射方向 = { 1.0f, 0.0f, 0.0f };
	
	return body;
}

void f_PBF销毁流体body(S_PBF流体* body)
{
	delete body->m_体素;
	free(body);
}

S_PBF刚体* f_PBF创建刚体body() {
	S_PBF刚体* body = (S_PBF刚体*)malloc(sizeof(S_PBF刚体));
	body->m_体素 = new S_体素();

	body->m_Offset = 0;
	body->m_Count = 0;
	body->m_密度 = 0.8;

	return body;
}

void f_PBF销毁刚体body(S_PBF刚体* body)
{
	delete body->m_体素;
	free(body);
}

S_PBF粒子发射* f_PBF创建粒子发射() {
	S_PBF粒子发射* body = (S_PBF粒子发射*)malloc(sizeof(S_PBF粒子发射));
	body->m_体素 = new S_体素();

	body->m_Offset = 0;
	body->m_Count = 0;

	body->m_偏移 = 0;
	body->m_数量 = 0;
	
	return body;
}

void f_PBF销毁粒子发射(S_PBF粒子发射* body)
{
	delete body->m_体素;
	free(body);
}






